package org.itheima.googleplay.ui.fragment;

import java.util.List;

import org.itheima.googleplay.bean.SubjectBean;
import org.itheima.googleplay.http.SubjectProtocol;
import org.itheima.googleplay.ui.adapter.SuperBaseAdapter;
import org.itheima.googleplay.ui.fragment.LoadingPager.LoadedResult;
import org.itheima.googleplay.ui.holder.BaseHolder;
import org.itheima.googleplay.ui.holder.SubjectHolder;
import org.itheima.googleplay.ui.weiget.BaseListView;
import org.itheima.googleplay.utils.UIUtils;

import android.view.View;
import android.widget.AbsListView;

/**
 * @包名: org.itheima.googleplay.ui.fragment
 * @类名: SubjectFragment
 * @作者: 肖琦
 * @创建时间 : 2015-3-22 下午4:34:24
 * 
 * @描述: 专题页面
 * 
 * @SVN版本: $Rev: 16 $
 * @更新人: $Author: xq $
 * @更新时间: $Date: 2015-03-23 12:16:33 +0800 (星期一, 23 三月 2015) $
 * 
 * @更新描述:
 */
public class SubjectFragment extends BaseFragment
{
	private SubjectProtocol		mProtocol;
	private List<SubjectBean>	mListDatas;

	@Override
	protected View onSuccessView()
	{
		BaseListView listView = new BaseListView(UIUtils.getContext());

		// 设置adapter
		listView.setAdapter(new SubjectAdapter(listView, mListDatas));

		return listView;
	}

	@Override
	protected LoadedResult onLoadData()
	{
		mProtocol = new SubjectProtocol();

		try
		{
			mListDatas = mProtocol.loadData(0);

			return checkState(mListDatas);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return LoadedResult.ERROR;
		}
	}

	class SubjectAdapter extends SuperBaseAdapter<SubjectBean>
	{

		public SubjectAdapter(AbsListView listView, List<SubjectBean> datas) {
			super(listView, datas);
			// TODO Auto-generated constructor stub
		}

		@Override
		protected BaseHolder<SubjectBean> getHolder(int position)
		{
			return new SubjectHolder();
		}

		@Override
		protected List<SubjectBean> onLoadMoreData() throws Exception
		{
			return loadMoreData(mDatas.size());
		}
	}

	private List<SubjectBean> loadMoreData(int index) throws Exception
	{
		return mProtocol.loadData(index);
	}
}
